package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.foolishbird.sdk.assist.CallbackObservable;
import com.foolishbird.sdk.assist.Constant;
import com.foolishbird.sdk.handler.SimpleAsyncResponseHandler;
import com.foolishbird.sdk.params.BindPhoneResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.activity.AppBaseActivity;
import com.foolishbird.sdk.ui.view.DialogFactory;
import com.foolishbird.sdk.ui.view.ProgressDialog;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.ToolsUtils;
import com.foolishbird.sdk.utils.WL;


public class BaseDialogFragment extends DialogFragment {
    private static final String TAG = BaseDialogFragment.class.getSimpleName();
    private int screenHeight = ViewGroup.LayoutParams.MATCH_PARENT;
    private static final int REQUEST_PERMISSIONS = 0;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        if(context instanceof Activity){
            Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
            screenHeight = Math.max(screenHeight, display.getHeight());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().addFlags( WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN| WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, screenHeight);
        setCancelable(false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected final void checkAppSelfPermission(String permission) {
        if (!ToolsUtils.checkAppSelfPermission(getContext(), permission)) {
            requestPermissions(new String[]{permission}, REQUEST_PERMISSIONS);
        }
    }

    protected final int getViewIdentifier(String name) {
        return ResUtils.getViewIdentifier(getContext(), name);
    }

    protected final int getLayoutIdentifier(String name) {
        return ResUtils.getLayoutIdentifier(getContext(), name);
    }

    protected final int getStringIdentifier(String name) {
        return ResUtils.getStringIdentifier(getContext(), name);
    }

    protected final int getDrawableIdentifier(String name) {
        return ResUtils.getDrawableIdentifier(getContext(), name);
    }

    protected final int getStyleIdentifier(String name) {
        return ResUtils.getStyleIdentifier(getContext(), name);
    }

    protected View doInflateContentView(LayoutInflater inflater, ViewGroup root, int resource) {
        if (inflater == null) {
            if (!isNullContext()) {
                inflater = LayoutInflater.from(getContext());
            } else {
                return null;
            }
        }
        return inflater.inflate(resource, root);
    }

    protected boolean isNullContext() {
        return getContext() == null;
    }

    /**
     * 获取应用程序名称
     */
    protected final String getAppName() {
        return ToolsUtils.getAppName(getContext());
    }

    protected Bundle getNotNullArguments(){
        Bundle bundle = getArguments();
        if(bundle == null){
            bundle = new Bundle();
        }
        return bundle;
    }

    @Override
    public void onStart() {
        super.onStart();
        this.isStoped = false;
        WL.d(TAG,"onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        this.isStoped = false;
        WL.d(TAG,"onResume");
    }
    private boolean isStoped = true;
    @Override
    public void onStop() {
        super.onStop();
        this.isStoped = true;
        WL.d(TAG,"onstop");
    }


}
